import {
    addChargeSumit,
    approvalCharge,
    cancelChargeConfirm,
    chargeAddApproval,
    confirmCharge,
    getById,
    getListForPage,
    updateOrderDeclare,
    updateOrderMain,
    updateOrderSi,
    updateOrderTrailer
} from '../../services/logisticsOrder/order';
import {message} from 'antd';
import Constants from '../../utils/Constants'

const R = require('ramda');

export default {
    namespace: 'order',
    state: {
        page: {},
        list: [],
        dataSource: {},
        booking: {},
        company: {},
        deliveryReceipt: {},
        odChargeMap: [],
        odDeclare: {},
        odSi: {},
        odTrailer: {},
        order: {},
        suppliers: {}
    },
    reducers: {
        /**
         * 刷新state
         * 
         * @param state 
         */
        refreshState(state, { payload }) {
            return { ...state, ...payload };
        }
    },
    effects: {
        /**
         * 获取订单详情
         *
         * @param receiptId
         * @return
         */
        *getById({ payload }, { call, put }) {
            if (payload.receiptId) {
                const res = yield call(getById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if (res && res.data.resultCode === "ok") {
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content,
                            booking: res.data.content.booking ? res.data.content.booking : {},
                            company: res.data.content.company ? res.data.content.company : {},
                            deliveryReceipt: res.data.content.deliveryReceipt ? res.data.content.deliveryReceipt : {},
                            odChargeMap: res.data.content.odChargeMap ? res.data.content.odChargeMap : {},
                            odDeclare: res.data.content.odDeclare ? res.data.content.odDeclare : {},
                            odSi: res.data.content.odSi ? res.data.content.odSi : {},
                            odTrailer: res.data.content.odTrailer ? res.data.content.odTrailer : {},
                            order: res.data.content.order ? res.data.content.order : {},
                            suppliers: res.data.content.suppliers ? res.data.content.suppliers : {}
                        }
                    });
                } else {
                    if (payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: {}
                    }
                });
            }
        },
        /**
         * 更新订单主界面信息
         *
         * @param orderMainForm
         * @param bindingResult
         * @return
         */
        *updateOrderMain({ payload }, { call, put }) {
            const res = yield call(updateOrderMain, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info(" 主界面信息更新成功！");
                if (payload.onSuccess) payload.onSuccess(res.data.content.id);
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 更新订单拖车
         *
         * @param odTrailerForm
         * @param bindingResult
         * @return
         */
        *updateOrderTrailer({ payload }, { call, put }) {
            const res = yield call(updateOrderTrailer, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("拖车信息更新成功！");
                if (payload.onSuccess) payload.onSuccess(res.data.content.id);
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 更新订单报关
         *
         * @param odDeclareForm
         * @param bindingResult
         * @return
         */
        *updateOrderDeclare({ payload }, { call, put }) {
            const res = yield call(updateOrderDeclare, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("报关信息更新成功！");
                if (payload.onSuccess) payload.onSuccess(res.data.content.id);
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 更新订单补料
         *
         * @param odSiForm
         * @param bindingResult
         * @return
         */
        *updateOrderSi({ payload }, { call, put }) {
            const res = yield call(updateOrderSi, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("补料信息更新成功！");
                if (payload.onSuccess) payload.onSuccess(res.data.content.id);
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 操作确认费用
         *
         * @param id
         * @return
         */
        *confirmCharge({ payload }, { call, put }) {
            const res = yield call(confirmCharge, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("费用确认成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 操作取消费用确认
         *
         * @param id
         * @return
         */
        *cancelChargeConfirm({ payload }, { call, put }) {
            const res = yield call(cancelChargeConfirm, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("取消费用确认成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 业务审核费用
         *
         * @param id
         * @return
         */
        *approvalCharge({ payload }, { call, put }) {
            const res = yield call(approvalCharge, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("审批结果已提交！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 费用变更新增审核
         *
         * @param id
         * @return
         */
        *chargeAddApproval({ payload }, { call, put }) {
            const res = yield call(chargeAddApproval, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("审批结果已提交！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 费用变更新增提交
         *
         * @param id
         * @return
         */
        *addChargeSumit({ payload }, { call, put }) {
            const res = yield call(addChargeSumit, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("变更费用提交成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 分页查询订单
         *
         * @param serviceType
         * @param orderNo
         * @param soNo
         * @param drBeginTime
         * @param drEndTime
         * @param loadingPort
         * @param destinationPort
         * @param cstName
         * @param status
         * @param gdUserName
         * @param cntrNo
         * @param sealNumber
         * @param page
         * @return
         */
        *getListForPage({ payload }, { call, put }){
            const res = yield call(getListForPage, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type:'refreshState',
                    payload: {
                        list: res.data.content.list,
                        page: res.data.content.page
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        }
    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};